//
//  WBMaskScreenController.m
//  WorkBreak
//
//  Created by Wei Nan Li on 11/10/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "WBScreenMasker.h"


@implementation WBScreenMasker

- (id) init
{
	self = [super init];
	if (self != nil) {
		screenWindows = [[NSMutableArray arrayWithCapacity:1] retain]; // assume user has one screen
	}
	return self;
}

- (void)destroyMaskForAllScreens {
	[screenWindows removeAllObjects];
}

- (void)setupMaskForAllScreens {	

	NSArray *screens = [NSScreen screens];
	NSEnumerator *screenEnumerator = [screens objectEnumerator];
	NSScreen* screen;
	NSWindow* screenWindow;
	NSView* screenWindowView;
	while (screen = [screenEnumerator nextObject]) {		
		NSRect winRect = [screen frame];
		/* for secondary screen, the origin is set to value other than 0, which will lead wrong view position */
		winRect.origin.x = 0;
		winRect.origin.y = 0;
		
		screenWindow = [[NSWindow alloc] initWithContentRect:winRect
												   styleMask:NSBorderlessWindowMask
													 backing:NSBackingStoreBuffered
													   defer:NO
													  screen:screen];
		[screenWindow setBackgroundColor:[NSColor blackColor]];	
		screenWindowView = [[NSView alloc]
							initWithFrame:winRect];
		[screenWindow setContentView:screenWindowView];
		[screenWindow setAlphaValue:0.1];
		[screenWindow setLevel:NSFloatingWindowLevel];
		[screenWindowView setNeedsDisplay:YES];
		[screenWindows addObject:screenWindow];
	}
}

- (void)unmaskAllScreens {
	NSEnumerator *enumerator = [screenWindows objectEnumerator];
	NSWindow *window;
	while (window = [enumerator nextObject]) {
		[window orderOut:self];
	}
}

- (void)maskAllScreens {
	NSEnumerator *enumerator = [screenWindows objectEnumerator];
	NSWindow *window;
	while (window = [enumerator nextObject]) {
		[window makeKeyAndOrderFront:self];
	}
}

- (void) dealloc
{
	[screenWindows dealloc];
	[super dealloc];
}

@end
